/**
 * web2010_admin
 * cn.aresoft.web.action.system.user RoleAction.java
 */
package com.xsoft.web.action.sys.role;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


import com.xsoft.framework.util.Page;
import com.xsoft.logic.model.sys.Menu;
import com.xsoft.logic.model.sys.Module;
import com.xsoft.logic.model.sys.ModuleFunction;
import com.xsoft.logic.model.sys.Role;
import com.xsoft.logic.model.sys.UserRolePrivilege;
import com.xsoft.logic.service.interfaces.sys.module.ModuleService;
import com.xsoft.logic.service.interfaces.sys.role.RoleService;
import com.xsoft.web.action.AdminAction;


/**
 * @author 潘冬 2010-8-10 上午10:00:06
 */
public class RoleAction extends AdminAction {
	
	private RoleService roleService;
	private ModuleService moduleService;
	private static String FoldName = "system/Role";
	private Role vo = new Role();
	private List<ModuleFunction> funcateinList;
	private List<Role> roleList;
	private List<Module> moduleList;
	private String type = "0";

	public String getType() {
		return type;
	}

	public List<Module> getModuleList() {
		return moduleList;
	}

	public void setModuleList(List<Module> moduleList) {
		this.moduleList = moduleList;
	}

	public List<ModuleFunction> getFuncateinList() {
		return funcateinList;
	}


	public void setFuncateinList(List<ModuleFunction> funcateinList) {
		this.funcateinList = funcateinList;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public Role getVo() {
		return vo;
	}

	public void setVo(Role vo) {
		this.vo = vo;
	}


	/**
	 * Insert
	 * 
	 * @author 潘冬 2010-8-10 下午01:11:03
	 * @return insert成功后返回beforeUpdate，失败返回beforeInsert
	 */
	public String insert() {
		roleService.insert(vo);
		createCommonResult(0,"ok",null);
		return SUCCESS;
	}

	/**
	 * 更新用户角色
	 * @return
	 */
	public String updateUserRole(){
		Long userId = Long.parseLong(request.getParameter("id"));
		String roleIds = request.getParameter("roleIds");
		roleService.updateUserRole(userId,roleIds);
		createCommonResult(0,"ok",null);
		return SUCCESS;
	}
	
	
	/**
	 * 进入设置权限
	 * @return
	 */
	public String beforeSavePrivilege() {
		
		moduleList = moduleService.getAllModule();
		
		Long Id = Long.parseLong(request.getParameter("id"));
		String type = request.getParameter("type");
		
		HashMap map1 =  new HashMap();
		map1.put("Id", Id);
		map1.put("type", type);
		
		ArrayList<UserRolePrivilege> menuRefList = (ArrayList<UserRolePrivilege>)roleService.getUserRolePrivilege(map1);
		if (menuRefList == null || menuRefList.size() == 0) {
			return SUCCESS;
		}
		
		for(Module module : moduleList) {
			List<Menu> menuList =  module.getMenus();
			for(int i=0;i<menuList.size();i++) {
				Menu menu = menuList.get(i);
				for(int j=0;j<menuRefList.size();j++){
					if(menuRefList.get(j).getMenuId().longValue()==menu.getId().longValue()){
						menu.setSelected("1");
					}
				}
			}
		}
		return SUCCESS;
	}
	
	/**
	 * 设置权限
	 */
	public String updatePrivilege(){
		Long Id = Long.parseLong(request.getParameter("id"));
		String type = request.getParameter("type");
		String menuIds = request.getParameter("menuIds");
		if(Id!=null && menuIds!=null){
			roleService.updatePrivilege(Id,type,menuIds);
		}
		createCommonResult(0,"ok",null);
		return SUCCESS;
	}
	
	

	public String select() {
		Page page = roleService.getDateByPage(gotoPage, pageSize, vo);
		roleList = (List<Role>) page.getResult();
		this.setPageInfo(page);
		return SUCCESS;
	}
	
	public String selectRoleById() {
		Role vo = (Role)roleService.load(Long.parseLong(request.getParameter("id")));
		createCommonResult(0,"ok",vo);
		return SUCCESS;
	}
	
	public String delete() {
		roleService.delete(Long.parseLong(request.getParameter("id")));
		roleService.deleteRolePrivilege(Long.parseLong(request.getParameter("id")));
		createCommonResult(0,"ok",null);
		return SUCCESS;
	}
	
	public String update() {
		roleService.update(vo);
		createCommonResult(0,"ok",null);
		return SUCCESS;
	}  


	@Override
	protected void initModuleFunction() {
		// TODO Auto-generated method stub
		super.initModuleFunction();
		this.functions.put("saveFunction", "设置权限");
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public ModuleService getModuleService() {
		return moduleService;
	}

	public void setModuleService(ModuleService moduleService) {
		this.moduleService = moduleService;
	}


}
